package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardBattleRecordData;
import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardCalExtraResult;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardLevelConfig;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardRoomConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogShuiHuYeZiCardBattlerResultRecord;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.manger.ShuiHuYeZiCardCommonMgr;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import io.netty.channel.ChannelHandlerContext;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/20 14:39
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_SETTLEMENT_FROM_CROSS, desc = "水浒叶子牌 pvp结算 跨服返回区服 协议")
public class ShuiHuYeZiCardPvpSettlementCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardSettlementCrossToGameMsg crossMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardSettlementCrossToGameMsg.parseFrom(packet.getBytes());
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(packet.getPlayerId());
        if(player == null) {
            //  玩家不在线
            ShuiHuYeZiCardMgr.getLogger().info("叶子牌结算 不在线的玩家:{},rankScore:{},isWin:{},diffLevel:{},remainCardNum:{},roomId:{},targetRemainCardNum:{},targetId:{},startTime:{},endTime:{}",packet.getPlayerId(),crossMsg.getRankScore(),crossMsg.getIsWin(),crossMsg.getDiffLevel(),crossMsg.getRemainCardNum(),crossMsg.getRoomId(),crossMsg.getTargetRemainCardNum(),crossMsg.getTargetId(),crossMsg.getStartTime(),crossMsg.getEndTime() );
        }else {
            ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
            if (crossMsg.getRoomId() != shuiHuYeZiCardModule.getPvpRoomId()) {
                ShuiHuYeZiCardMgr.getLogger().info("水浒叶子牌活动，区服记录玩家pvp房间id 与 跨服下发的id 。不一致。userId:{},roomId:{},crossMsg.roomId:{}", packet.getPlayerId(), shuiHuYeZiCardModule.getPvpRoomId(), crossMsg.getRoomId());
            }
            ShuiHuYeZiCardBattleRecordData battleRecordData = new ShuiHuYeZiCardBattleRecordData();
            if(ShuiHuYeZiCardMgr.getActivityInfo() != null){
                battleRecordData.setActivityId(ShuiHuYeZiCardMgr.getActivityInfo().getActivityId());
            }
            battleRecordData.setUserId(player.getUserId());
            battleRecordData.setRoomType(crossMsg.getRoomId());
            battleRecordData.setRemainCardNum(crossMsg.getRemainCardNum());
            battleRecordData.setIsWin(crossMsg.getIsWin());
            // 对手
            battleRecordData.setTargetRemainCardNum(crossMsg.getTargetRemainCardNum());
            battleRecordData.setTargetInfo(PlayerBasePb.parseToUserBaseInfo(crossMsg.getPlayerInfo()));
            battleRecordData.setStartTime(crossMsg.getStartTime());
            battleRecordData.setEndTime(crossMsg.getEndTime());

            ShuiHuYeZiCardRoomConfig roomConfig = ShuiHuYeZiCardMgr.getConfig().getRoomConfigMap().get(crossMsg.getRoomId());

            String reward = null;
            //  参赛时的段位
            int level = ShuiHuYeZiCardCommonMgr.getLevel(ShuiHuYeZiCardMgr.getConfig(),crossMsg.getRankScore());
            ShuiHuYeZiCardLevelConfig levelConfig = ShuiHuYeZiCardMgr.getConfig().getLevelConfigMap().get(level);
            shuiHuYeZiCardModule.addBattleWinLose(crossMsg.getRankScore(),crossMsg.getIsWin(),false,0,crossMsg.getRoomId());
            int addRankScore = 0;
            // 原来的段位积分
            int oldRankScore = shuiHuYeZiCardModule.getUserData().getRankScore();
            int winStreakNumPlus = 0;
            int remainCardPlus = 0;
            if(crossMsg.getIsWin()){
                // 获胜
                addRankScore = levelConfig.getSucceedScore() * roomConfig.getScoreParam();
                reward = roomConfig.getSucceedReward();
                // 额外的分数加成
                ShuiHuYeZiCardCalExtraResult result = ShuiHuYeZiCardMgr.calExtraRankScore(shuiHuYeZiCardModule.getUserData().getPveWinningStreakNum(),crossMsg.getRemainCardNum(),roomConfig.getScoreParam());
                addRankScore += result.getTotalPlus();
                winStreakNumPlus = result.getWinStreakNum();
                remainCardPlus = result.getRemainCardPlus();
            }else {
                // 失败
                addRankScore = -levelConfig.getDefeatScore() * roomConfig.getScoreParam();
                reward = roomConfig.getDefeatReward();
            }
            shuiHuYeZiCardModule.addRankScore(addRankScore);

            ShuiHuYeZiCardMgr.addReward(player, reward, eLogMoneyType.ShuiHuYeZiCardActivityPvpEndGameReward);

            shuiHuYeZiCardModule.setPvpRoomId(0);
            battleRecordData.setAddRankScore(addRankScore);
            battleRecordData.setCurRankScore(shuiHuYeZiCardModule.getUserData().getRankScore());
            battleRecordData.setReward(reward);
            battleRecordData.setInsertOption();
            shuiHuYeZiCardModule.addShuiHuYeZiCardBattleRecordData(battleRecordData);
            // 广播结算奖励
            ShuiHuYeZiCardMgr.sendSettlement(6,player,crossMsg.getIsWin(),reward,oldRankScore,shuiHuYeZiCardModule.getUserData().getRankScore(),shuiHuYeZiCardModule.getUserData().getPvpWinningStreakNum(),winStreakNumPlus,remainCardPlus);

            // 加日志
            LogMgr.addLogShuiHuYeZiCardBattlerResultRecord(new LogShuiHuYeZiCardBattlerResultRecord(ShuiHuYeZiCardMgr.getActivityInfo().getActivityId(),
                    player.getUserId(), 1, crossMsg.getRoomId(), crossMsg.getIsWin(), crossMsg.getTargetId(),
                    oldRankScore, shuiHuYeZiCardModule.getUserData().getRankScore(), reward, crossMsg.getStartTime(),
                    DateHelper.getCurrentSecond()));

            shuiHuYeZiCardModule.synUserData();
        }
    }
}
